<?php

namespace App\Admin\Controllers;

use App\Models\Configs;
use App\Models\Messages;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ConfigRichTextController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '富文本配置';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Configs());
        $grid->model()->where('value_type', 'rich_text');
        $grid->column('describe', '说明')->width(250);
        $grid->column('value', '内容')->display(function ($content) {
            return mb_substr(str_replace("&nbsp;", "", strip_tags($content)), 0, 100).'...';
        });

        $grid->column('updated_at', '修改时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Messages::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Configs);

        $form->display('describe', '说明')->readonly();
        $form->hidden('value_type')->default('rich_text');
        $form->editor('value', '内容')->required();

        $form->footer(function ($footer) {
            $footer->disableReset();
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        $form->tools(function ($tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }
}
